package org.imlinker.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;

import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JTextField;

import org.imlinker.status.StatusCellRenderer;
import org.imlinker.status.StatusManager;
import org.imlinker.util.Images;

@SuppressWarnings("serial")
public class StatusDialog extends javax.swing.JDialog {

	public class ListItem {

		public ListItem(final ImageIcon paraIcon, final String paraTitle) {
			this.icon = paraIcon;
			this.title = paraTitle;
		}

		public ImageIcon getIcon() {
			return icon;
		}

		public String getTitle() {
			return title;
		}

		private ImageIcon icon;

		private String title;

	}

	/**
	 * 生成新的状态设置对话框
	 * 
	 * @param statusManager
	 */
	public StatusDialog(final JFrame window, final StatusManager statusManager) {
		super(window, true);

		initComponents();

		ImageIcon icon = statusManager.getIcon();
		for (int i = 0; i < iconListModel.getSize(); i++) {
			if (icon.equals(((ListItem) iconListModel.getElementAt(i))
					.getIcon())) {
				iconListModel.setSelectedItem(iconListModel.getElementAt(i));

				break;
			}
		}

		ArrayList<String> statuses = statusManager.getStatuses();
		for (String status : statuses) {
			statusListModel.addElement(status);
		}

		jComboBox1.addItemListener(new ItemListener() {

			public void itemStateChanged(final ItemEvent e) {
				statusManager.setStatus(((ListItem) e.getItem()).getIcon(),
						null);
			}

		});

		jComboBox2.addActionListener(new ActionListener() {

			public void actionPerformed(final ActionEvent e) {
				String status = ((JTextField) jComboBox2.getEditor()
						.getEditorComponent()).getText();
				statusManager.setStatus(null, status);

				if (e.getActionCommand().endsWith("Edited")) {
					StatusDialog.this.setVisible(false);
					StatusDialog.this.dispose();
				}
			}

		});

		jButton1.addActionListener(new ActionListener() {

			public void actionPerformed(final ActionEvent e) {
				StatusDialog.this.setVisible(false);
				StatusDialog.this.dispose();
			}

		});

		setLocationRelativeTo(null);

		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(final WindowEvent e) {
				StatusDialog.this.dispose();
			}
		});
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		jComboBox1 = new javax.swing.JComboBox();
		jComboBox2 = new javax.swing.JComboBox();
		jComboBox2.setEditable(true);
		jLabel1 = new javax.swing.JLabel();
		jLabel2 = new javax.swing.JLabel();
		jButton1 = new javax.swing.JButton();

		java.util.ResourceBundle bundle = java.util.ResourceBundle
				.getBundle("org/imlinker/gui/Bundle"); // NOI18N

		iconListModel = new DefaultComboBoxModel();
		iconListModel.addElement(new ListItem(Images.getAvailableIcon(),
				"Available"));
		iconListModel.addElement(new ListItem(Images.getAwayIcon(), "Away")); 
		iconListModel.addElement(new ListItem(Images.getDndIcon(), "Dnd")); 
		jComboBox1.setModel(iconListModel);
		jComboBox1.setRenderer(new StatusCellRenderer());

		statusListModel = new DefaultComboBoxModel();
		jComboBox2.setModel(statusListModel);

		setTitle(bundle.getString("StatusDialog.title"));

		jLabel1.setText(bundle.getString("StatusDialog.statusicon"));
		jLabel2.setText(bundle.getString("StatusDialog.statusinfo"));

		jButton1.setText(bundle.getString("StatusDialog.okbutton"));

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout
				.setHorizontalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(jLabel1)
														.addComponent(jLabel2))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(
																jComboBox2, 0,
																244,
																Short.MAX_VALUE)
														.addComponent(
																jComboBox1, 0,
																244,
																Short.MAX_VALUE))
										.addGap(18, 18, 18).addComponent(
												jButton1).addContainerGap()));
		layout
				.setVerticalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jLabel1)
														.addComponent(
																jComboBox1,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addGap(18, 18, 18)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jLabel2)
														.addComponent(jButton1)
														.addComponent(
																jComboBox2,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addContainerGap(
												javax.swing.GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)));

		pack();
	}// </editor-fold>

	// Variables declaration - do not modify
	private javax.swing.JButton jButton1;

	private javax.swing.JComboBox jComboBox1;

	private javax.swing.JComboBox jComboBox2;

	private javax.swing.JLabel jLabel1;

	private javax.swing.JLabel jLabel2;

	private DefaultComboBoxModel iconListModel;

	// End of variables declaration

	private DefaultComboBoxModel statusListModel;

}
